package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.mapper.CheckGroupCheckItemDao;
import com.itheima.mapper.CheckItemDao;
import com.itheima.pojo.CheckItem;
import com.itheima.service.ICheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ICheckItemServiceImpl implements ICheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Autowired
    private CheckGroupCheckItemDao checkGroupCheckItemDao;

    /**
     * 新增检查项
     *
     * @param checkItem
     * @return
     */
    @Override
    public Result add(CheckItem checkItem) {
        return checkItemDao.add(checkItem) > 0 ? Result.of(true, "新增检查项成功", null) : Result.of(false, "新增检查项失败", null);
    }

    /**
     * 根据id删除检查项
     *
     * @param id
     * @return
     */
    @Override
    public Result delete(String id) {
        CheckItem checkItem = checkItemDao.findById(id);
        if (checkItem == null) {
            return Result.of(false, "参数有误!", null);
        }
        //查询该项目包含在几个项目组中
        Integer count = checkGroupCheckItemDao.getCheckGroupCountByCheckItemId(id);
        if (count > 0) {
            return Result.of(false, "检查组中包含该项目无法删除!", null);
        }

        //删除
        return checkItemDao.deleteById(id) > 0 ? Result.of(true, "删除检查项成功", null) : Result.of(false, "删除检查项失败", null);
    }

    /**
     * 根据检查组id查询关联的检查项id
     *
     * @param checkGroupId
     * @return
     */
    @Override
    public Result findCheckItemIdsByCheckGroupId(String checkGroupId) {
        List<Integer> checkItemIds = checkGroupCheckItemDao.findCheckItemIdsByCheckGroupId(checkGroupId);
        return Result.of(true, "查询检查项成功", checkItemIds);
    }

    /**
     * 编辑检查项
     *
     * @param checkItem
     * @return
     */
    @Override
    public Result edit(CheckItem checkItem) {
        if (checkItem == null || checkItem.getId() == null) {
            return Result.of(false, "参数有误!", null);
        }
        return checkItemDao.edit(checkItem) > 0 ? Result.of(true, "编辑检查项成功", null) : Result.of(false, "编辑检查项失败", null);

    }

    /**
     * 分页查询检查项
     *
     * @param query
     * @return
     */
    @Override
    public PageResult<CheckItem> findPage(QueryPageBean query) {
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());

        List<CheckItem> checkItemList = checkItemDao.findByCondition(query.getQueryString());

        PageInfo<CheckItem> pageInfo = new PageInfo<>(checkItemList);
        long total = pageInfo.getTotal();
        return new PageResult<>(total, checkItemList);
    }

    /**
     * 查询所有检查项
     *
     * @return
     */
    @Override
    public Result findAll() {
        List<CheckItem> checkItemList = checkItemDao.findAll();
        return Result.of(true, "查询检查项成功", checkItemList);
    }

    /**
     * 根据id查询指定检查项
     *
     * @param id
     * @return
     */
    @Override
    public Result findById(String id) {
        CheckItem checkItem = checkItemDao.findById(id);
        return Result.of(true, "查询检查项成功", checkItem);
    }
}
